package com.webchat.chat.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.webchat.chat.core.baseEntity.Result;
import com.webchat.chat.core.baseEntity.ResultGenerator;
import com.webchat.chat.core.dto.customerDto.UserDto;
import com.webchat.chat.core.redis.RedisService;
import com.webchat.chat.core.tools.ChatConstant;
import com.webchat.chat.core.tools.RedisConstant;
import com.webchat.chat.core.tools.ShiroUtil;
import com.webchat.chat.model.ChatRecord;
import com.webchat.chat.model.SocketClient;
import com.webchat.chat.model.SocketRoom;
import com.webchat.chat.service.ChatRecordMGService;
import com.webchat.chat.service.ChatRecordService;
import com.webchat.chat.service.SocketClientService;
import com.webchat.chat.service.SocketRoomService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Owner on 2018/7/5.
 */

@RestController
@RequestMapping(ChatConstant.CONTROLLER_ROOT_PATH + "/chatAudit")
public class ChatAuditController  extends BaseController{

    @Resource
    private SocketClientService socketClientService;
    @Resource
    private SocketRoomService socketRoomService;
    @Resource
    private ChatRecordMGService chatRecordMGService;


    @PostMapping("/findCustomer")
    public Result findCustomer(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<UserDto> userList =socketClientService.findCustomer(ShiroUtil.getTenantCode());
        PageInfo pageInfo = new PageInfo(userList);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/changeCustomerAuditStatus")
    public Result changeCustomerAuditStatus(@RequestParam String[] userIds, Boolean flag) {
        socketClientService.changeCustomerAuditStatus(userIds,flag);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/findNeedAuditMessage")
    public Result findNeedAuditMessage(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
    //    PageHelper.startPage(page, size);
    //    List<ChatRecord> chatRecord = chatRecordService.findNeedAuditMessageByTenantCode(tenantCode);
        PageInfo pageInfo = chatRecordMGService.findNeedAuditMessage(page,size,ShiroUtil.getTenantCode());
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/messagePassAudit")
    public Result messagePassAudit(String[] chatRecordIds) {

        boolean flag = chatRecordMGService.messagePassAudit(chatRecordIds,ShiroUtil.getTenantCode());
        if (!flag){
            return ResultGenerator.genFailResult("程序异常,请联系管理员！！");
        }
        return ResultGenerator.genSuccessResult("消息审核通过！！");
    }


    @PostMapping("/getChatRoom")
    public Result getChatRoom(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SocketRoom>  chatRooms = socketRoomService.getChatRoom(ShiroUtil.getTenantCode());
        PageInfo pageInfo = new PageInfo(chatRooms);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/noticeRoom")
    public Result noticeRoom(String roomIds,Boolean flag) {
        socketRoomService.changePublicRoomToNotice(roomIds,flag);
        return ResultGenerator.genSuccessResult();
    }
}
